<template>
  <el-dialog
    @close="cancel"
    :title="title"
    :visible.sync="open"
    width="800px"
    class="dialogClass"
    append-to-body
  >
    <template slot="title">
      <div class="modalTitle">
        <img
          src="@/assets/bluelogo/drawerTitle.png"
          alt=""
          class="titleLogo"
        />{{ title }}
      </div>
    </template>
    <el-form
      ref="form"
      :model="formData"
      :rules="rules"
      size="small"
      label-width="135px"
    >
      <el-form-item label="年份" prop="trendYear">
        <el-date-picker
          clearable
          v-model="formData.trendYear"
          type="year"
          value-format="yyyy"
          placeholder="请选择年份"
          :maxlength="50"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="立案时间" prop="filingTime">
        <el-date-picker
          clearable
          v-model="formData.filingTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:ss:mm"
          placeholder="请选择立案时间"
          :picker-options="pickerOptions"
          :maxlength="50"
          popper-class="no-atTheMoment"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="案件名称" prop="caseName">
        <el-input
          v-model.trim="formData.caseName"
          placeholder="请输入案件名称"
          :maxlength="50"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="案件阶段" prop="caseStatus">
        <el-select v-model="formData.caseStatus" placeholder="请选择案件阶段" :maxlength="50">
          <el-option
            v-for="dict in dict.type.case_stage"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="案件详情" prop="caseDetails">
        <el-input v-model="formData.caseDetails"
                  type="textarea"
                  placeholder="请输入案件详情"
                  :maxlength="300"
                  show-word-limit
                  :autosize="{ minRows: 7 }" />
      </el-form-item>
      <el-form-item label="完成状态">
        <el-radio-group v-model="formData.compleStatus" :maxlength="50" disabled>
          <el-radio
            v-for="dict in dict.type.comple_status"
            :key="dict.value"
            :label="dict.value"
          >{{dict.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <div class="dialog-footer">
        <el-button @click="cancel" class="resetButton">取消</el-button>
        <el-button class="searchButton" @click="submitForm">确定</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import {
  listChen,
  getChen,
  delChen,
  addChen,
  updateChen,
} from "@/api/caseManage/caseChen/chen";

import {
  MODALTYPE,
  CASESTATE,
  PAGESIZE,
  PAGENUM,
  DEPTTYPE,
  ROLETYPE,
  CASEEXPORTURL,
} from "@/utils/variable";

import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  inheritAttrs: false,
  dicts: ["case_stage", "comple_status"],
  props: {
    modalType: {
      type: String,
      require: true,
    },
    id: {
      type: Number,
    },
  },

  data() {
    return {
      isDisabled: false,
      // 弹出层标题
      title: "",
      temCaseName: "",
      // 是否显示弹出层
      open: false,
      formData: {
        caseName: "",
        filingTime: "",
        caseStatus: "",
        caseDetails: "",
        compleStatus: "0",
      },
      rules: {
        trendYear: [
          { required: true, message: "年份不能为空", trigger: "blur" },
        ],
        caseName: [
          { required: true, message: "案件名称不能为空", trigger: "blur" },
        ],
        filingTime: [
          { required: true, message: "立案时间不能为空", trigger: "blur" },
        ],
        caseStatus: [
          { required: true, message: "案件阶段时间不能为空", trigger: "blur" },
        ],
        caseDetails: [
          { required: true, message: "案件详情不能为空", trigger: "blur" },
        ],
        compleStatus: [
          { required: true, message: "完成状态不能为空", trigger: "blur" },
        ],
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();  // 禁用当前时间之后的日期
        },
      },
    };
  },

  computed: {
    resetButtonContent() {
      if (this.modalType == MODALTYPE["详情"]) {
        return "关闭";
      } else {
        return "取消";
      }
    },
  },

  watch: {
    modalType: {
      handler(value) {
        if (value == MODALTYPE["新增"]) {
          this.reset();
          this.open = true;
          this.title = "新增陈案";
        } else if (value == MODALTYPE["修改"]) {
          this.reset();
          if (this.id) {
            getChen(this.id).then((response) => {
              this.formData = response.data;
              this.temCaseName = this.formData.caseName;
              this.open = true;
              this.title = "修改陈案";
            });
          }
        } else if (value == "detail") {
          this.isDisabled = true;
          this.reset();
          if (this.id) {
            getChen(this.id).then((response) => {
              this.formData = response.data;
              this.open = true;
              this.title = "案件详情";
            });
          }
        }
      },
      immediate: true,
    },
    'formData.caseStatus'(newVal) {
      console.log(newVal)
      // 根据案件阶段的值，修改完成状态的选中状态
      if (newVal === '0' || newVal === '1' || newVal === '2') {
        this.formData.compleStatus = "0"; // 选中完成
      } else if (newVal === '3') {
        this.formData.compleStatus = "1"; // 选中未完成
      } else {
        this.formData.compleStatus = ''; // 清空选择
      }
    }
  },

  methods: {
    // 取消按钮
    cancel() {
      this.$emit("update:modalType", "");
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formData = {
        caseName: "",
        filingTime: "",
        caseStatus: "",
        caseDetails: "",
        compleStatus: "0",
      };
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.modalType == MODALTYPE["修改"]) {
            updateChen(this.formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("update:modalType", "");
              this.$emit("fatherMethod");
            });
          } else if (this.modalType == MODALTYPE["新增"]) {
            // this.formData.caseState = CASEDEFAULTSTATE;
            addChen(this.formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("update:modalType", "");
              this.$emit("fatherMethod");
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../../../assets/styles/dialog.scss"; //模态框样式

::v-deep.treeselect {
  font-size: 12px !important;
  font-family: $font-family !important;
  font-weight: $form-label-font-weight !important;
}

.a >>> .el-picker-panel__link-btn {
    display: none
}
</style>
